package control

import (
	"chat_project/service"
	"chat_project/util"
	"fmt"
	"net/http"
)

var userService service.UserService
var returnJson util.ReturnJson

type UserControl struct {
}

func (UserControl) UserRegister(writer http.ResponseWriter, request *http.Request) {

	fmt.Println("进入control.user.userRegister函数")
	// 获取post请求的参数
	request.ParseForm()
	mobile := request.PostForm.Get("mobile")
	plainpwd := request.PostForm.Get("password")
	sex := request.PostForm.Get("sex")
	nickname := request.PostForm.Get("nickname")
	avatar := request.PostForm.Get("avatar")
	// 请求service层
	user, err := userService.Register(mobile, plainpwd, nickname, avatar, sex)
	if err != nil {
		returnJson.ReturnJsonStr(writer, -1, err.Error(), nil)
	} else {
		returnJson.ReturnJsonStr(writer, 0, "", user)
	}
}

func (UserControl) UserLogin(writer http.ResponseWriter, request *http.Request) {

	fmt.Println("进入control.user.userLogin函数")
	// 获取post请求的参数
	request.ParseForm()
	mobile := request.PostForm.Get("mobile")
	plainpwd := request.PostForm.Get("password")
	user, err := userService.Login(mobile, plainpwd)
	if err != nil {
		returnJson.ReturnJsonStr(writer, -1, err.Error(), nil)
	} else {
		returnJson.ReturnJsonStr(writer, 0, "", user)
	}

}
